﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using CLibArtreve.bll;
using CLibArtreve.models;
using CLibArtreve.security;


namespace WebAppArtreve.views.shared.components.arts
{
    public partial class WUCBuyArt : System.Web.UI.UserControl
    {
        //VARIABLES
        private static Int64 _artId = 0;
        private string _connectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings["ARTREVEConnectionString"].ConnectionString;
            }
        }
        private Int64? _userId
        {
            get
            {
                ArtreveIdentity identity = (ArtreveIdentity)Context.User.Identity;
                return identity.User.Id;
            }
        }

        //PROPERTIES
        public Int64 ArtId
        {
            get
            {
                return _artId;
            }
            set
            {
                _artId = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //0. Session
            //-----------------------
            if (!Context.User.Identity.IsAuthenticated)
            {
                // there is no identity, terminate this action
                throw new Exception("Unauthorised access!");
            }
        }

        //BUTTON (LIKE ART) CLICKED
        protected void buyArt_Click(object sender, EventArgs e)
        {            
            buyArt();
        }

        //ADD LIKES
        private void buyArt()
        {
            try
            {
                //1. BLL
                //-----------------------
                UserBLL bllUser = new UserBLL(_connectionString);
                ArtBLL bllArt = new ArtBLL(_connectionString);

                //2. DATA
                //----------------------- 
                //get art
                Art art = bllArt.GetArtById(_artId);
                //get users
                User userBuy = bllUser.GetUserById(_userId);
                User userSell = bllUser.GetUserById(art.UserId);

                //3. MODIFICATION
                //update tokens
                userBuy.Tokens -= Convert.ToInt16(art.Price);
                userSell.Tokens += Convert.ToInt16(art.Price);
                art.Numberof -= 1;

                //4. VALIDATION
                //enough tokens?
                if (userBuy.Tokens < 0)
                {
                    // User does not have enough tokens to buy this item
                    // We should add an error box, btw
                    throw new Exception("Not enough tokens");
                }

                // Enough Art?
                if (art.Numberof < 0)
                {
                    // There's not enough art left to make this transaction.
                    // This item is obsolete, delete it
                    DeleteArt(art);
                    // Let the user know the deal's off
                    throw new Exception("Not enough art left");
                }

                // Time to save things up
                bllUser.UpdateUser(userBuy);
                bllUser.UpdateUser(userSell);
                bllArt.UpdateArt(art);
                Response.AppendHeader("Refresh", "0;URL=Myartreve.aspx");
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        private void DeleteArt(Art art)
        {
            ArtBLL bllArt = new ArtBLL(_connectionString);
            art.DeletedDate = DateTime.Now;
            bllArt.UpdateArtDeletedDate(art);
        }
    }
}